package com.example.schooljava.controller;


import com.example.schooljava.annotation.RequestLimit;
import com.example.schooljava.pojo.bo.UploadBO;
import com.example.schooljava.pojo.dto.*;
import com.example.schooljava.pojo.entity.Teacher;
import com.example.schooljava.pojo.bo.StudentsOfClassBO;
import com.example.schooljava.pojo.bo.StudentsScoreOfClassBO;
import com.example.schooljava.pojo.bo.TeachersBO;
import com.example.schooljava.service.impl.TeacherServiceImpl;
import com.example.schooljava.util.DTOtoBO;
import com.example.schooljava.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LDH
 * @since 2022/08/11
 */
@RestController
@RequestMapping("/school/teacher")
@RequestLimit(maxCount = 5,second = 2)
@Api(tags = "教师模块")
public class TeacherController {

    @Autowired
    private TeacherServiceImpl service;

    @Autowired
    private DTOtoBO dtOtoBO;

    @ApiOperation(value = "教师登录",notes = "<span style='color:red;'>详细描述：</span>&nbsp;教师登录功能")
    @PostMapping(value = "/login", name = "")
    public Result teacherLogin(@RequestBody Teacher teacher){
        return service.teacherLogin(teacher.getAccount(),teacher.getPassword());
    }

    @ApiOperation(value = "教师注册",notes = "<span style='color:red;'>详细描述：</span>&nbsp;教师注册功能")
    @PostMapping(value = "/register", name = "")
    public Result teacherRegister(@RequestBody Teacher teacher){
        return service.teacherRegister(teacher);
    }


    @ApiOperation(value = "班主任查找学生信息",notes = "<span style='color:red;'>详细描述：</span>&nbsp;班主任根据各种条件查询学生相关信息")
    @PostMapping(value = "/allScore", name = "班主任")
    public Result findAllScore(@RequestBody StudentsOfClassDTO studentsOfClassDTO){
        StudentsOfClassBO studentsOfClassBO = new StudentsOfClassBO();
        if (studentsOfClassDTO.getCurriculum() != null && !Objects.equals(studentsOfClassDTO.getCurriculum(), ""))
            return service.findOneScore((StudentsOfClassBO) dtOtoBO.copyPro(studentsOfClassDTO,studentsOfClassBO));
        return service.findAllScore((StudentsOfClassBO) dtOtoBO.copyPro(studentsOfClassDTO,studentsOfClassBO));
    }

    @ApiOperation(value = "查询某班所有教师",notes = "<span style='color:red;'>详细描述：</span>&nbsp;班主任查询本班的所有科任老师")
    @GetMapping(value = "/teacherList", name = "班主任")
    public Result getTeacherList(@RequestParam Long teacher){
        return service.getTeacherList(teacher);
    }

    @ApiOperation(value = "查询不同班级的学生",notes = "<span style='color:red;'>详细描述：</span>&nbsp;科任老师查询自己所教的所有班级的学生成绩")
    @PostMapping(value = "/studentofcurr", name = "科任老师")
    public Result getStudentOfCurr(@RequestBody StudentsOfClassDTO studentsOfClassDTO){
        StudentsOfClassBO studentsOfClassBO = new StudentsOfClassBO();
        return service.getStudentOfCurr((StudentsOfClassBO) dtOtoBO.copyPro(studentsOfClassDTO, studentsOfClassBO));
    }

    @ApiOperation(value = "查询年级",notes = "<span style='color:red;'>详细描述：</span>&nbsp;科任老师查询自己有教的年级")
    @GetMapping(value = "/grade", name = "科任老师")
    public Result getGrade(@RequestParam Long teacherid){
        return service.getGrade(teacherid);
    }

    @ApiOperation(value = "查询班级名",notes = "<span style='color:red;'>详细描述：</span>&nbsp;科任老师查询某个年级内自己有教的班级名")
    @GetMapping(value = "classname", name = "科任老师")
    public Result getClassName(@RequestParam Long teacherid,@RequestParam Integer grade){
        return service.getClassName(teacherid,grade);
    }

    @ApiOperation(value = "获取全校所有教师",notes = "<span style='color:red;'>详细描述：</span>&nbsp;学校管理人员获取全校教师信息")
    @PostMapping(value = "/teachers", name = "校长")
    public Result getTeachers(@RequestBody TeachersDTO teachersDTO){
        TeachersBO teachersBO = new TeachersBO();
        return service.getTeacher((TeachersBO) dtOtoBO.copyPro(teachersDTO,teachersBO));
    }

    @ApiOperation(value = "删除教师信息",notes = "<span style='color:red;'>详细描述：</span>&nbsp;学校管理人删除教师信息")
    @DeleteMapping(value = "/delteachers", name = "校长")
    public Result delTeachers(@RequestBody ArrayList<String> list){
        return service.delectTeacher(list);
    }

    @ApiOperation(value = "获取学生成绩",notes = "<span style='color:red;'>详细描述：</span>&nbsp;班级助理获取本班同学成绩")
    @PostMapping(value = "/getScore", name = "班级助理")
    public Result getScore(@RequestBody StudentsScoreOfClassDTO studentsScoreOfClassDTO){
        StudentsScoreOfClassBO studentsScoreOfClassBO = new StudentsScoreOfClassBO();
        return service.getStudentScoreOfClass((StudentsScoreOfClassBO) dtOtoBO.copyPro(studentsScoreOfClassDTO,studentsScoreOfClassBO));
    }

    @ApiOperation(value = "获取路由",notes = "<span style='color:red;'>详细描述：</span>&nbsp;获取用户身份对应的路由")
    @GetMapping(value = "/router", name = "")
    public Result getRouter(@RequestParam String uid){
        Long tno = Long.parseLong(uid);
        return service.getRouter(tno);
    }

    @ApiOperation(value = "上传头像",notes = "<span style='color:red;'>详细描述：</span>&nbsp;用户上传头像，不设权限")
    @PostMapping("/upload")
    public Result saveUpload(@RequestBody @Validated UploadDTO uploadDTO){
        UploadBO uploadBO = new UploadBO();
        return service.saveUpload((UploadBO) dtOtoBO.copyPro(uploadDTO,uploadBO));
    }

    @ApiOperation(value = "获取头像",notes = "<span style='color:red;'>详细描述：</span>&nbsp;用户获取头像，不设权限")
    @GetMapping("/upload")
    public Result getUpload(@RequestParam String tno){
        return service.getUpload(tno);
    }
}
